<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

use  App\Models\AppSetting;
use  App\Models\AppSettingPhone;
use App\Models\Phone;
use App\Models\PhoneGroup;
use Illuminate\Foundation\Auth\User;
use App\Models\WeixinGroup;

class AppSettingController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('str');
        $validator = Validator::make($input, [        
            'str'     =>  'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $app_setting = AppSetting::where('id','>',0);
        if(isset($input['str'])){
            
        }
        $count = $app_setting->count();
        $data =  $app_setting->get();
        foreach ($data as $key => $value) {
            $data[$key]['count'] = AppSettingPhone::where('app_setting_id',$value['id'])->count();
        }
        return $this->returnOk($data);
    }

    public function create()
    {
        $input = Input::only('name','package_name');
        $validator = Validator::make($input, [        
            'name' => 'required',
            'package_name' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $data = $input;
        $where['name'] = $input['name'];
        $exist = AppSetting::where($where)->first();
        if($exist){
            return $this->returnError([],409,'APP已存在');
        }else{
            AppSetting::create($data);
        }
        return $this->returnOk();
    }

    public function update()
    {
        $input = Input::only('id','name','package_name');
        $validator = Validator::make($input, [ 
            'id' => 'required',
            'name' => 'required',
            'package_name' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['id'] = $input['id'];
        $exist = AppSetting::where($where)->first();
        if($exist){
            $exist->name = $input['name'];
            $exist->package_name = $input['package_name'];
            $exist->save();
        }else{
            return $this->returnError([],409,'APP不存在');
        }
        return $this->returnOk();
    }

    public function delete()
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [ 
            'id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['id'] = $input['id'];
        $exist = AppSetting::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'APP不存在');
        }
        return $this->returnOk();
    }

    public function addPhone()
    {
        $input = Input::only('id','ids');
        $validator = Validator::make($input, [ 
            'id' => 'required',
            'ids' => 'required|array',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['app_setting_id'] = $input['id'];
        $exist = AppSettingPhone::where($where)->delete();
        $id = $input['id'];
        $ids = $input['ids'];
        foreach ($ids as $key => $value) {
            $data['app_setting_id'] = $id;
            $data['phone_id'] = $value;
            AppSettingPhone::create($data);
        }
        return $this->returnOk();
    }

    public function phoneList()
    {
        $input = Input::only('app_id');
        $validator = Validator::make($input, [ 
            'app_id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $where['app_setting_id'] = $input['app_id'];
        $phone_ids = AppSettingPhone::where($where)->pluck('phone_id');
        $phone_ids = $phone_ids?$phone_ids->toArray():[];
        $phone = Phone::get();
        $group = PhoneGroup::select('name','id')->get();
        $temp['name'] = '未分组';
        $temp['id'] = 0;
        $gData = $group->toArray();
       
        array_push($gData,$temp);
        foreach ($phone as $key => $value) {
            $flag = 0;
            $value['is_bind'] = 0;
            if(in_array($value['id'],$phone_ids)){
                $value['is_bind'] = 1;
            }
            foreach ($gData as $k => $val) {
                $gData[$k]['group_data'] = [];
                if($value['group_id']==$val['id']){
                    $flag = 1;
                    $gData[$k]['group_data'][] = $value;
                }
            }
            if(!$flag){
                $gData[count($group)-1]['group_data'][] = $value;
            }
        }
        return $this->returnOk($gData);
    }
}

